<?php
/*
Plugin Name: Permisos para usuarios
Plugin URI: 
Description: Permite hacer visible un post a solo un usuario.
Author: 
Author URI: 
*/

add_action('add_meta_boxes', 'creacion_meta_box');

function creacion_meta_box() {
	add_meta_box('meta_box_authors', 'Permiso para profesores', 'contenido_profesores', 'post');
}

/*Imprime la Meta Box en la administracion de los posts */

function contenido_profesores() {

	/* Get the users based on role. */
    $users = get_users();
	
	/* Check if any users were returned. */
    if ( is_array( $users ) ) {
		
		$nombre_profesor = get_post_meta( $post->ID, 'meta_box_authors', true );?>
		
	    <p>Visible para: 
	    <select name="meta_profesores">
	    	<option value="default_todos">Todos</option>


		<?php
        /* Loop through each user. */
        foreach ( $users as $user ) {?>
		    <option value="vampire" <?php selected( $nombre_profesor, $user->display_name ); ?>><?php echo $user->display_name; ?></option>
		<?php
        }?>
        
	    </select>
	    </p>
    <?php   
    }
}?>

<?php

add_action( 'save_post', 'save_MetaData' );

add_action( 'edit_post', 'save_MetaData' );
/* When the post is saved, saves our custom data */
function save_MetaData( $post_id ) {

  // First we need to check if the current user is authorised to do this action. 
	if ( ! current_user_can( 'edit_post', $post_id ) )
        return;
	
  // Secondly we need to check if the user intended to change this value.
  if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
      return;

  // Thirdly we can save the value to the database

  //if saving in a custom table, get post_ID
  $post_ID = $_POST['post_ID'];
  //sanitize user input
  $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );

  // Do something with $mydata 
  // either using 
  add_post_meta($post_ID, 'meta_user', $mydata, true) ||  update_post_meta($post_ID, 'meta_user', $mydata);
  // or a custom table (see Further Reading section below)
}?>